<template>
  <div class="vehicle-list-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>车辆列表</h2>
          <div>
            <el-button type="primary" @click="fetchVehicles">刷新</el-button>
            <el-button type="success" @click="dialogVisible = true">添加车辆</el-button>
          </div>
        </div>
      </template>
      <el-table :data="vehicles" style="width: 100%">
        <el-table-column prop="id" label="车辆ID" width="80" />
        <el-table-column prop="plateNumber" label="车牌号" width="120" />
        <el-table-column prop="ownerId" label="业主姓名" width="120">
          <template #default="scope">
            <span>{{ getOwnerName(scope.row.ownerId) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" title="添加车辆" width="400px">
      <el-form :model="form">
        <el-form-item label="车牌号">
          <el-input v-model="form.plateNumber" required></el-input>
        </el-form-item>
        <el-form-item label="业主">
          <el-select v-model="form.ownerId" placeholder="请选择业主">
            <el-option v-for="owner in owners" :key="owner.id" :label="owner.realName || owner.username" :value="owner.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addVehicle">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import request from '../utils/request'

const vehicles = ref([])
const dialogVisible = ref(false)
const form = ref({ plateNumber: '', ownerId: '', status: 1 })
const owners = ref([])

const fetchVehicles = async () => {
  try {
    const res = await request.get('http://localhost:9999/vehicle-server/api/vehicle/all')
    vehicles.value = res.data
  } catch (e) {
    ElMessage.error('获取车辆列表失败')
  }
}

const fetchOwners = async () => {
  try {
    const res = await request.get('http://localhost:9999/owner-server/queryOwner')
    owners.value = res.data
  } catch (e) {
    ElMessage.error('获取业主信息失败')
  }
}

function getOwnerName(ownerId) {
  const owner = owners.value.find(o => o.id === ownerId)
  return owner ? owner.realName || owner.username : ownerId
}

const addVehicle = async () => {
  if (!form.value.plateNumber) {
    ElMessage.error('车牌号不能为空')
    return
  }
  try {
    const res = await request.post('http://localhost:9999/vehicle-server/api/vehicle/add', form.value)
    if (res.data === true) {
      ElMessage.success('添加成功')
      dialogVisible.value = false
      form.value.plateNumber = ''
      form.value.ownerId = ''
      form.value.status = 1
      fetchVehicles()
    } else {
      ElMessage.error('添加失败')
    }
  } catch (e) {
    ElMessage.error('添加失败')
  }
}

onMounted(() => {
  fetchVehicles()
  fetchOwners()
})
</script>

<style scoped>
.vehicle-list-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 